<template>
  <div>
    <TopFilter :list="topFilterList" :cols="4" :labelWidth="'90'" @filterChange="changeHandle"></TopFilter>
    <FilterTable ref="table" :columns="columns" :fetchapi="fetchApi" :params="fetchParams" :datakey="'rows'" :onColumnsChange="columns => (this.columns = columns)"></FilterTable>
    <BaseDialog :visible.sync="visiblemxdetail" :title="dialogTitle" :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }" destroyOnClose>
      <viewPackageDetails :detaildata="detaildata" @detailsAddActivity="detailsAddActivity" />
    </BaseDialog>
  </div>
</template>
<script>
// 主查询、查询车系
import { getAllList, findProjectModelList, findProjectSeriesList } from '@/api/repair/activitiesManage/activitiesPackageQuery';
import { dictionary } from '@/utils/dictMixin';
import viewPackageDetails from './viewPackageDetails';

export default {
  name: 'activitiesPackageQuery',
  components: {
    viewPackageDetails
  },
  mixins: [dictionary],
  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createTopFilterList(),
      fetchApi: getAllList,
      visiblemxdetail: false,
      fetchParams: { xhrAbort: true },
      columns: this.createTableColumns(),
      detaildata: '',
      dialogTitle: '套餐明细查看',
      formData: {}
    };
  },
  mounted() {
    this.getChexSelect();
  },
  methods: {
    createTopFilterList() {
      return [
        {
          type: 'INPUT',
          label: '套餐代码',
          fieldName: 'PACKAGE_CODE',
          placeholder: '套餐代码...'
        },
        {
          type: 'INPUT',
          label: '套餐名称',
          fieldName: 'PACKAGE_NAME',
          placeholder: '套餐名称...'
        },
        {
          type: 'SELECT',
          label: '套餐类型',
          fieldName: 'PACKAGE_TYPE',
          placeholder: '套餐类型',
          itemList: this.createDictList('8104')
        },
        {
          type: 'SELECT',
          label: '车系',
          fieldName: 'SERIES_CODE',
          placeholder: '车系',
          itemList: [],
          rules: [{ required: false, message: '请选择车系', trigger: 'change' }],
          change: val => {
            this.changeModelSelect(val);
          }
        },
        {
          type: 'SELECT',
          label: '车型',
          fieldName: 'MODEL_CODE',
          placeholder: '请选择',
          itemList: []
        },
        {
          type: 'SELECT',
          label: '车辆用途',
          fieldName: 'VEHICLE_PURPOSE',
          placeholder: '请选择',
          itemList: this.createDictList('8026')
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 100,
          fixed: 'left',
          render: props => {
            return (
              <div>
                <el-button size="mini" type="text" onClick={() => this.detail(props.row)}>
                  明细
                </el-button>
              </div>
            );
          }
        },
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '套餐代码',
          dataIndex: 'PACKAGE_CODE',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '套餐名称',
          dataIndex: 'PACKAGE_NAME',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '套餐类型',
          dataIndex: 'PACKAGE_TYPE',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'input',
          render: (props, h) => {
            return <span> {this.createDictText(props.row.PACKAGE_TYPE, '8104')} </span>;
          }
        },
        {
          title: '车系',
          dataIndex: 'SERIES_NAME',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车型',
          dataIndex: 'MODEL_NAME',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车辆用途',
          dataIndex: 'VEHICLE_PURPOSE',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '工时',
          dataIndex: 'EFFECTIVE_TIME',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '工时费',
          dataIndex: 'LABOUR_AMOUNT',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '材料费',
          dataIndex: 'REPAIR_PART_AMOUNT',
          sorter: true,
          filter: true
        },
        {
          title: '总次数',
          dataIndex: 'TOTAL_TIMES',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '第次',
          dataIndex: 'NUMBER_TIME',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '是否免费',
          dataIndex: 'IS_FREE',
          sorter: true,
          // editable: true,
          // defaultEditable: true,
          // editType: 'checkbox',
          // editItems: [{ text: '', trueValue: '10041001' }, { text: '', falseValue: '10041002' }],
          render: props => {
            let val = this.ischange(props.row.IS_FREE) === 1 ? true : false;
            return <el-checkbox value={val} onInput={val => (props.row.IS_FREE = val ? 1 : 0)}></el-checkbox>;
          }
        },
        {
          title: '是否启用',
          dataIndex: 'STATUS',
          sorter: true,
          render: props => {
            let val = this.ischange(props.row.STATUS) === 1 ? true : false;
            return <el-checkbox value={val} onInput={val => (props.row.STATUS = val ? 1 : 0)}></el-checkbox>;
          }
        },
        {
          title: '停用日期',
          dataIndex: 'DISABLE_DATE',
          sorter: true,
          filter: true,
          width: 150,
          filterType: 'input',
          dateFormat: 'yyyy-MM-dd HH:mm:ss'
        },
        {
          title: '备注',
          dataIndex: 'REMARK',
          sorter: true,
          filter: true,
          editType: 'input',
          editable: true
        }
      ];
    },
    async changeModelSelect(val) {
      let projectModelList = (await findProjectModelList(val)).data.map(x => ({ text: x.MODEL_NAME, value: x.MODEL_CODE }));
      this.topFilterList.find(x => x.fieldName === 'MODEL_CODE').itemList = projectModelList;
    },
    async getChexSelect() {
      let projectModelList = (await findProjectSeriesList()).data.map(x => ({ text: x.SERIES_NAME, value: x.SERIES_CODE }));
      this.topFilterList.find(x => x.fieldName === 'SERIES_CODE').itemList = projectModelList;
    },
    changeHandle(val) {
      this.fetchParams = Object.assign({}, val, { xhrAbort: false });
    },
    // 对后台取出来的值进行转换
    ischange(sql) {
      if (sql == 10041001) {
        return 1;
      } else {
        return 0;
      }
    },
    // 明细弹框开启
    detail(val) {
      this.detaildata = val;
      console.log('987654564987', val);
      this.visiblemxdetail = true;
    },
    // 明细弹框关闭
    detailsAddActivity() {
      this.visiblemxdetail = false;
    }
  }
};
</script>
